package com.copera.cn.kaka;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;

import com.copera.cn.kaka.util.UploadUtil;

public class PhotoActivity extends BaseActivity {
	private final static String ALBUM_PATH = Environment
			.getExternalStorageDirectory() + "/kaka/";

	private String TEMP_FILE = "";
	private String type;

	public String buildFileName() {
		Date now = new Date();
		SimpleDateFormat formater = new SimpleDateFormat("yyMMddHHmmss");

		File dir = new File(ALBUM_PATH);
		dir.mkdirs();

		return ALBUM_PATH + formater.format(now) + ".jpg";
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		try {
			TEMP_FILE = buildFileName();
            type=getIntent().getStringExtra("type");
			Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 调用摄像头拍照
			intent.putExtra(MediaStore.EXTRA_OUTPUT,
					Uri.fromFile(new File(TEMP_FILE))); // 另存图片地址
			startActivityForResult(intent, 1);

		} catch (Exception e) {

		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		try {
			if (resultCode == Activity.RESULT_OK && requestCode == 1) {
				File file = new File(TEMP_FILE);
				int jd = readPictureDegree(file.getAbsolutePath());
				if (file.exists()) {

					Toast.makeText(PhotoActivity.this, "图片已保存在：" + TEMP_FILE, 10000).show();
					BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
					bitmapOptions.inSampleSize = 8;
					Bitmap cameraBitmap = BitmapFactory.decodeFile(TEMP_FILE,
							bitmapOptions);
					// UploadUtil.saveMyBitmap(cameraBitmap);
					Intent data1 = new Intent();
                    if(type==null){
                    	//店铺banner
                    	getAppCustomer().setBmystp(file.getAbsolutePath());
    					setResult(10, data1);
    					getAppCustomer().setBmImage(
    							UploadUtil.rotaingImageView(jd, cameraBitmap));
    					getAppCustomer().setBmjd(jd + "");
                    }else if(type.equals("head")){
                    	//店铺头像
                    	getAppCustomer().setZmystp(file.getAbsolutePath());
    					setResult(20, data1);
    					getAppCustomer().setImage(
    							UploadUtil.rotaingImageView(jd, cameraBitmap));
    					getAppCustomer().setZmjd(jd + "");
                    }else if(type.equals("shopImage")){
                    	//店铺头像
                    	getAppCustomer().setShopystp(file.getAbsolutePath());
    					setResult(30, data1);
    					getAppCustomer().setShopImage(
    							UploadUtil.rotaingImageView(jd, cameraBitmap));
    					getAppCustomer().setShopjd(jd + "");
                    }else if(type.equals("banner")){
                    	//店铺广告
                    	getAppCustomer().setGgystp(file.getAbsolutePath());
    					setResult(40, data1);
    					getAppCustomer().setGgimage(
    							UploadUtil.rotaingImageView(jd, cameraBitmap));
    					getAppCustomer().setGgjd(jd + "");
    					getAppCustomer().setPhotoFile(TEMP_FILE);
                    }
					
				}
			}
		} catch (Exception err) {
			Log.e("Exception", err.getLocalizedMessage());
		}

		finish();
	}

	
	public static int readPictureDegree(String path) {
		int degree = 0;
		try {
			ExifInterface exifInterface = new ExifInterface(path);
			int orientation = exifInterface.getAttributeInt(
					ExifInterface.TAG_ORIENTATION,
					ExifInterface.ORIENTATION_NORMAL);
			switch (orientation) {
			case ExifInterface.ORIENTATION_ROTATE_90:
				degree = 90;
				break;
			case ExifInterface.ORIENTATION_ROTATE_180:
				degree = 180;
				break;
			case ExifInterface.ORIENTATION_ROTATE_270:
				degree = 270;
				break;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return degree;
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		int i = ScreenOrient(this);
		if (i == 1) {
			getAppCustomer().setTakeType(1);
		}

		Log.e("FHT", "onConfigurationChanged:" + "屏幕翻转");
		super.onConfigurationChanged(newConfig);
	}

	public static int ScreenOrient(Activity activity) {
		// 取得当前屏幕的方向，如果此值为-1表示androidManifest.xml没有设置Android:screanOrentation属性所以这样无法判断屏幕方向。
		// 可以使用另一种思路，即长度大于高度的为横屏，否则为竖屏。

		int width = activity.getWindowManager().getDefaultDisplay().getWidth();// 得到系统显示属性后得到屏幕宽度
		int height = activity.getWindowManager().getDefaultDisplay()
				.getHeight();// 得到屏幕高度
		return width > height ? 1 : 2;// 判断
	}

}
